package com.yzj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzj.pojo.Emp;
import com.yzj.service.DeptService;
import com.yzj.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class empController {
    @Autowired
    private EmpService es;
    @Autowired
    private DeptService ds;
    @RequestMapping("/addInsert")
    public String insert(Emp emp, Model model){
        model.addAttribute("dept",ds.list());
        return "insert";
    }
    @RequestMapping("/add")
    public String add(Emp emp){
        if (es.save(emp)){
            //model.addAttribute("emps",es.selectAll(1,2));
            return "redirect:/selectAll/1/3";
        }
            return "insert";
    }
    @RequestMapping("/selectAll/{pageIndex}/{pageSize}")
    public String selectAll(Model model,@PathVariable int pageIndex,@PathVariable int pageSize){
        model.addAttribute("emps",es.selectAll(new Page<>(pageIndex,pageSize)));
        return "index";
    }
    @RequestMapping("/update/{empno}")
    public  String update(Model model,@PathVariable int empno){
        System.out.println("dsdsd"+empno);
        model.addAttribute("dept",ds.list());
       model.addAttribute("updates",es.getById(empno));
        return "update";
    }
    @RequestMapping("/updates")
    public String updates(Emp emp){
        System.out.println(emp);
        if (es.updateById(emp)){
            return "redirect:/selectAll/1/3";
        }
        return "update";
    }
}
